var _pagination = null;
// 页面缓存
var _cache = null;
// 答案的缓存
var _uestionnaireCache = null;
// 当前操作题目Id
var _questionnaire = null;
/**
 * 入口，和服务器的通信从此开始
 */
$(function() {
	refreshInput($(".serWrapper"));
	$('.doubledate').kuiDate({
		className : 'doubledate',
		isDisabled : "0"
	});

});

/**
 * 为查找做准备，这里主要是生成查询条件对象
 */
function toSearch() {
	var content = $("#searchContent").val();
	var effectDate = $("#startDate").val();
	var unEffectDate = $("#endDate").val();
	var status = $("#searchStatus").val();
	_pagination = new Pagination({
		url : '../questionnaire/list',
		data : {
			name : content,
			effectDate : effectDate,
			unEffectDate : unEffectDate,
			status : status
		},
		success : printDataToPage,
		pageSize : 10
	});
}

function printDataToPage(message) {
	var response = new qmsk.Response(message);
	_cache = response;
	// "id","name","createFileTime","effectDate","uneffectDate","status"
	qmsk.dataTable({
		tbodyId : 'questionList',
		heads : [ "questionnaireId", "questionnaireName", "createFileTime", "effectDate", "uneffectDate",
				"status", "questionnaireId" ],
		response : response,
		handler : [ [ 5, statusHandler ], [ 6, detail ] ],
		sup : [ 'sup', '共 [index] 条' ]

	});
}

function statusHandler(status, row) {
	return status == 0 ? '正常' : '停用';
}

function detail(id, row) {
	var str = "<input class='edit common_btn' type='button' value='修改' onclick='toEdit("
			+ row + ")'>";
	str += "<input class='edit common_btn' type='button' value='配置'  onclick='configProblem("
			+ id + ")'>";
	return str;
}

function toAdd() {
	$("#name").val('');
	$("#effectDate").val('');
	$("#unEffectDate").val('');
	$("#status").val(0).attr("disabled", "disabled");
	$("#window_change,#yinying1").show();
	addMargin($("#window_change"));
	$("#addOrSaveButton").unbind('click');
	$("#addOrSaveButton").bind('click', add);
}

function add() {
	var name = $("#name").val();
	var effectDate = $("#effectDate").val();
	var unEffectDate = $("#unEffectDate").val();
	if (IsEmpty(name)) {
		artAlert("主题不能为空！");
		return false;
	}
	if (IsEmpty(effectDate)) {
		artAlert("生效日期不能为空！");
		return false;
	}

	if (IsEmpty(unEffectDate)) {
		artAlert("失效日期不能为空！");
		return false;
	}

	qmsk.post("../questionnaire/insert", {
		name : name,
		effectDate : effectDate,
		unEffectDate : unEffectDate
	}, function() {
		artSuccess("添加成功！");
		hideAddPanel();
		if (_pagination) {
			_pagination.reload();
		}
	});
}

function toEdit(row) {
	// "id","name","createFileTime","effectDate","uneffectDate","status"
	var id = _cache.getValue("id", row);
	var name = _cache.getValue("name", row);
	var effectDate = _cache.getValue("effectDate", row).substring(0, 10);
	var uneffectDate = _cache.getValue("uneffectDate", row).substring(0, 10);
	var status = _cache.getValue("status", row);
	$("#name").val(name);
	$("#effectDate").val(effectDate);
	$("#unEffectDate").val(uneffectDate);
	$("#status").val(status).removeAttr('disabled');

	$("#window_change,#yinying1").show();
	addMargin($("#window_change"));
	$("#addOrSaveButton").unbind('click');
	$("#addOrSaveButton").bind('click', {
		id : id
	}, edit);

}

function edit(event) {
	var id = event.data.id;

	var name = $("#name").val();
	var effectDate = $("#effectDate").val();
	var unEffectDate = $("#unEffectDate").val();
	var status = $("#status").val();
	if (IsEmpty(name)) {
		artAlert("主题不能为空！");
		return false;
	}
	if (IsEmpty(effectDate)) {
		artAlert("生效日期不能为空！");
		return false;
	}

	if (IsEmpty(unEffectDate)) {
		artAlert("失效日期不能为空！");
		return false;
	}

	qmsk.post("../questionnaire/update/" + id, {
		name : name,
		effectDate : effectDate,
		unEffectDate : unEffectDate,
		status : status
	}, function() {
		artSuccess("修改成功！");
		hideAddPanel();
		if (_pagination) {
			_pagination.reload();
		}
	});
}

function configProblem(id) {
	_questionnaire = id;
	loadProblem();
	$("#problemPanel").show();
	$("#questionPanel").hide();
}

function loadProblem() {
	qmsk.get('../questionnaire/problem/list/' + _questionnaire, function(
			response) {
		_uestionnaireCache = response;
		// "problemId","content","type","createFileTime"
		qmsk.dataTable({
			tbodyId : 'problemList',
			heads : [ "problemId", "content", "typeName", "problemId" ],
			response : response,
			handler : [ [ 3, deleteButton ] ],
			sup : [ 'problemSup', '共 [index] 条' ]
		});
	});
}

function deleteButton(problemId) {
	return "<input class='edit common_btn del' type='button' value='删除' onclick='unBindProblem("
			+ problemId + ")'>";
}

function typeTurn(code) {
	if (code == 'radio')
		return '单选';
	if (code == 'checkbox')
		return '多选';
	if (code == 'text')
		return '文本';
	if (code == 'select')
		return '下拉框';
}

function delteProblem(problemId) {
	qmsk.post('../answer/delete/' + answerId, function() {
		artSuccess("删除成功");
		loadAnswer();
	});
}

function initProblemSelecter() {
	initSelecter({
		url : '../problem/query',
		heads : [ "content", "typeName" ],
		titles : [ "描述", "类型" ],
		zindex : 500, // 缺省值
		callBack : problemSelected
	});
}

function problemSelected(response, row) {
	var problemId = response.getValue('problemId', row);
	qmsk.post('../questionnaire/bind/problem/' + _questionnaire + '/'
			+ problemId, function() {
		loadProblem();
	});
}

function unBindProblem(problemId) {
	qmsk.post('../questionnaire/unbind/problem/' + _questionnaire + '/'
			+ problemId, function() {
		loadProblem();
	});
}

/**
 * 隐藏添加弹出层
 */
function hideAddPanel() {
	$("#window_change,#yinying1").hide();
}

function closeAddOrUpdateAnswerPanel() {
	$("#answerAddUpdatePanel").hide();
	loadAnswer();
}

function configAnswer(problemId) {
	_problemId = problemId;
	loadAnswer();
	$("#answerPanel").show();
	$("#problemPanel").hide();
}

function closeProblemPanel() {
	$("#problemPanel").hide();
	$("#questionPanel").show();
}
